package com.wealth.system.web.controller;

import iaas.redis.RedisService;
import iaas.utils.ResultObject;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wealth.sms.enums.MessageTemplateEnum;
import com.wealth.sms.service.IMessageChannelService;

/** 
 * 短消息控制器
 * 
 * @author CHAN
 *  
 */
@Controller
@RequestMapping(value="/message")

public class MessageController extends BaseController {
	
	Logger logger = LoggerFactory.getLogger(MessageController.class);
	
	@Autowired
	private IMessageChannelService messageChannelService;
	
	@Autowired
	private RedisService redisService;
	
	/**
	 * 手机验证码
	 */
	@RequestMapping(value="/verifycode",method=RequestMethod.POST)
	@ResponseBody
	public ResultObject verifycode(@RequestParam String username){
			
		try {
			/** -------- 验证码生成 -------- **/
			String random = RandomStringUtils.random(6, "0123456789");
			
			/** -------- 将手机号,验证码存储到redis -------- **/
			redisService.set("redis_sms_"+username, random, 1200);//60s 20min
			
			/** -------- 发送消息 -------- **/
			Map<String, String> map = new HashMap<String, String>();
			map.put("code", random);
			ResultObject rs = messageChannelService.send(corpid, MessageTemplateEnum.VERIFYCODE.getValue(), username, map);
			return rs;
		} catch (Exception e) {
			logger.error("发送验证码失败", e);
			return new ResultObject("999", "发送验证码失败", null);
		}
	}
	
}
